package com.opencloud.iot.provider.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.opencloud.common.model.PageParams;
import com.opencloud.iot.client.model.entity.IotBeacons;
import com.opencloud.iot.client.model.entity.IotNStreng;
import com.opencloud.iot.provider.mapper.IotBeaconsMapper;
import com.opencloud.iot.provider.mapper.IotNStrengMapper;
import com.opencloud.iot.provider.service.IotBeaconsService;
import com.opencloud.iot.provider.service.IotNStrengService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 动量服务
 * @author yeyong
 */

@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class IotBeaconsServiceImpl implements IotBeaconsService {
    @Autowired
    private IotBeaconsMapper iotBeaconsMapper;

    @Override
    public IPage<IotBeacons> findListPage(PageParams pageParams) {
        IotBeacons query = pageParams.mapToObject(IotBeacons.class);
        QueryWrapper<IotBeacons> queryWrapper = new QueryWrapper();
        queryWrapper.lambda().eq(ObjectUtils.isNotEmpty(query.getMacaddress()),IotBeacons::getMacaddress,query.getMacaddress());
        queryWrapper.lambda().eq(ObjectUtils.isNotEmpty(query.getSerial()),IotBeacons::getSerial,query.getSerial());
        //queryWrapper.lambda().between(ObjectUtils.isNotEmpty(startTime) && ObjectUtils.isNotEmpty(endTime),IotNStreng::getTimestamp, startTime, endTime);
        //queryWrapper.lambda().between(ObjectUtils.isNotEmpty(startTime) && ObjectUtils.isNotEmpty(endTime),IotNStreng::getTimestamp, startTime, endTime);
        queryWrapper.lambda().orderByDesc(IotBeacons::getCreatetime);
        return iotBeaconsMapper.selectPage(new Page(pageParams.getPage(),pageParams.getLimit()),queryWrapper);
    }

    @Override
    public Integer addBeacons(PageParams pageParams)
    {
        IotBeacons obj = pageParams.mapToObject(IotBeacons.class);
        return iotBeaconsMapper.insert(obj);
    }
}
